import React, { Component, PropTypes } from "react"
import { messages } from "share/common"
import { Popover, Badge } from 'antd'

const DataHoc = WrappedComponent => {

  class hoc extends Component {

    setWrappedInstance = ref => {
      this.wrappedInstance = ref;
    };

    levelCode = {
      dataIndex: 'levelCode', title: messages('finance-4.key377')/*层级*/,
      render: () => messages('finance-4.key20')/*公司*/
    };

    companyName = { dataIndex: 'companyName', title: messages('finance-4.key346')/*所属组织*/ };

    accountNumber = { dataIndex: 'accountNumber', title: messages('finance-4.key347')/*企业支付宝账户*/ };

    bankName = { dataIndex: 'bankName', title: messages('finance-4.key22')/*银行名称*/ };

    countryCode = { dataIndex: 'countryCode', title: messages('finance-4.key366')/*国家*/ };

    bankAddress = { dataIndex: 'bankAddress', title: messages('finance-4.key367')/*开户地*/ };

    accountOpeningAddress = { dataIndex: 'accountOpeningAddress', title: messages('finance-4.key51')/*银行详细地址*/ };

    bankAccountName = { dataIndex: 'bankAccountName', title: messages('finance-4.key49')/*银行账户名称*/ };

    bankAccountNumber = { dataIndex: 'bankAccountNumber', title: messages('finance-4.key50')/*银行账户账号*/ };

    currencyName = { dataIndex: 'currencyName', title: messages('common.currency')/*币种*/, width: 72 };

    remark = {
      dataIndex: 'remark', title: messages('common.remark')/*备注*/, render: remark => (
        <Popover content={remark}>
          {remark}
        </Popover>)
    };

    isEnabled = {
      title: messages('common.column.status')/*状态*/, dataIndex: 'isEnabled',
      render: value => <Badge
        status={value ? 'success' : 'error'}
        text={value ? messages('finance-4.key344')/*启用temp*/ : messages('finance-4.key345')/*禁用temp*/}
      />
    };

    enabled = {
      title: messages('common.column.status')/*状态*/, dataIndex: 'enabled',
      render: value => <Badge
        status={value ? 'success' : 'error'}
        text={value ? messages('finance-4.key344')/*启用temp*/ : messages('finance-4.key345')/*禁用temp*/}
      />
    };

    authToken = {
      dataIndex: 'authToken', title: messages('finance-4.key378')/*授权状态*/,
      render: value => <Badge
        status={value ? 'success' : 'error'}
        text={value ? messages('finance-4.key379')/*已授权*/ : messages('finance-4.key380')/*未授权*/}
      />
    };

    defaults = {
      title: messages('finance-4.key381')/*默认*/, dataIndex: 'defaults',
      render: value => <Badge
        status={value ? 'success' : 'error'}
        text={value ? messages('common.yes')/*是*/ : messages('common.no')/*否*/}
      />
    };

    companyNameSearch = {
      id: 'companyId', label: messages('finance-4.key20')/*公司*/, component: 'Chooser',
      props: {
        type: 'priority_company',
        labelKey: 'name',
        valueKey: 'id',
        single: true
      },
      dataIndex: 'companyName',
      parser: value => ({
        companyId: value ? value[0].id : undefined
      })
    };

    companyBankNameSearch = {
      id: 'companyBankName', dataIndex: 'bankAccountName', component: 'Input'
    };

    accountNameSearch = {
      id: 'companyBankName', dataIndex: 'bankAccountName', component: 'Input'
    };

    getColumns = key => {
      switch (key) {
        case 'bank-account':
          return [this.levelCode, this.companyName, this.bankName, this.countryCode, this.bankAddress, this.accountOpeningAddress,
            this.bankAccountName, this.bankAccountNumber, this.currencyName, this.remark, this.isEnabled, this.defaults];
        case 'ali-account':
          return [this.levelCode, this.companyName, this.accountNumber, this.remark, this.enabled, this.authToken];
        default:
          return []
      }
    };

    getSearch = key => {
      switch (key) {
        case 'bank-account':
          return [this.companyNameSearch, this.companyBankNameSearch];
        case 'ali-account':
          return [this.companyNameSearch, this.accountNameSearch];
        default:
          return []
      }
    };

    render() {
      let props = { ...this.props, ref: this.setWrappedInstance };
      return(
        <WrappedComponent
          getColumns={this.getColumns}
          getSearch={this.getSearch}
          context={this.context}
          {...props}
        />
      )
    }

  }

  hoc.contextTypes = {
    router: PropTypes.object
  };

  return hoc

};

export default DataHoc
